/*
 * @(#)BlobUtil.java	Mar 22, 2015
 *
 * @Company <Opportune Technology Development Company LTD.>
 */
package com.cn.system.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import oracle.sql.BLOB;
import oracle.sql.CLOB;

/**
 * @Project <Gezhi>
 * @version <1.0>
 * @Author  <chenjx>
 * @Date    <Mar 22, 2015>
 * @description 
 */
public class BlobUtil {
	
	protected static Logger logger = LoggerFactory.getLogger(BlobUtil.class);
	
	/** 
     * 将blob转化为byte[],可以转化二进制流的 
     * @param blob 
     * @return 
     */  
	public static byte[] blobToBytes(java.sql.Blob blob) {
		InputStream is = null;
		byte[] b = null;
		try {
			if (blob != null) {
				is = blob.getBinaryStream();
				b = new byte[(int) blob.length()];
				is.read(b);
			}
			return b;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				is = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return b;
	}
	
	/*
	 * public static byte[] SerializableBlobToBytes(SerializableBlob
	 * serializableBlob){ byte[] bytes; BufferedInputStream bis = null; try { bis =
	 * new BufferedInputStream(serializableBlob.getBinaryStream()); bytes = new
	 * byte[(int)serializableBlob.length()]; int len = bytes.length; int offest = 0;
	 * int read = 0; while(offest<len&&(read=bis.read(bytes, offest,
	 * len-offest))>0){ offest+=read; } } catch (Exception e) { bytes = null;
	 * e.printStackTrace(); } finally{ if(bis!=null){ try { bis.close(); } catch
	 * (IOException e) { e.printStackTrace(); } bis = null; } } return bytes; }
	 */
	
	/**
	 * oracle.sql.CLOB
	 * 待测试
	 * @param clob
	 * @return
	 */
	public static String CLOB2String(CLOB clob) {
		// Clob转换成String 的方法
		String content = null;
		StringBuffer stringBuf = new StringBuffer();
		try {
			int length = 0;
			Reader inStream = clob.getCharacterStream();
			// 取得大字侧段对象数据输出流
			char[] buffer = new char[10];
			while ((length = inStream.read(buffer)) != -1)
			// 读取数据库 //每10个10个读取
			{
				for (int i = 0; i < length; i++) {
					stringBuf.append(buffer[i]);
				}
			}
			inStream.close();
			content = stringBuf.toString();
		} catch (Exception ex) {
			System.out.println("ClobUtil.Clob2String:" + ex.getMessage());
		}
		return content;
	}
	
	/**
	 * oracle.sql.BLOB
	 * 待测试
	 * @param clob
	 * @return
	 */
	public static String BLOB2String(BLOB blob,final Object... chartSet){
		String blobString="";
		try {
			if(chartSet!=null && chartSet.length>0 ){
				blobString = new String(blob.getBytes(1, (int) blob.length()),(String)chartSet[0]);
			}else{
				blobString = new String(blob.getBytes(1, (int) blob.length()),"UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return blobString;
		
		/*StringBuffer newStr = new StringBuffer(""); // 返回字符串
		long blobLength; // BLOB字段长度
		byte[] bytes; // BLOB临时存储字节数组
        int i = 1; // 循环变量
		try {
			byte[] msgContent = blob.getBytes(); // BLOB转换为字节数组
			blobLength = blob.length(); // 获取BLOB长度
            if (msgContent == null || blobLength == 0) // 如果为空，返回空值
            {
                return "";
            } else {
                while (i < blobLength) // 循环处理字符串转换，每次1024；Oracle字符串限制最大4k
                {
                    bytes = blob.getBytes(i, 1024);
                    i = i + 1024;
                    newStr.append(new String(bytes, "UTF-8"));//UTF-8  gb2312
                }
            }
            //System.out.println("blobString==newStr:"+blobString.equals(newStr));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newStr;*/
		
		
	}
	
	/**
	 * serializableClob转字符串
	 * @param blob 待转换的对象
	 * @param chartSet 字符集,默认址为UTF-8,取值为GBK,UTF-8
	 * @return
	 */
	/*
	 * public static String serializableBlobToString(SerializableBlob blob,final
	 * Object... chartSet){ String blobString=""; try { if(chartSet!=null &&
	 * chartSet.length>0 ){ blobString = new String(blob.getBytes(1, (int)
	 * blob.length()),(String)chartSet[0]); }else{ blobString = new
	 * String(blob.getBytes(1, (int) blob.length()),"UTF-8"); } } catch (Exception
	 * e) { e.printStackTrace(); } return blobString; }
	 */
	/**
	 * serializableClob转字符串
	 * @param serializableClob
	 * @return
	 */
	/*
	 * public static String serializableClobToString(SerializableClob
	 * serializableClob){ Reader reader; StringBuffer sb = new StringBuffer(); try {
	 * reader = serializableClob.getCharacterStream(); BufferedReader br = new
	 * BufferedReader(reader); String temp = null; while ((temp = br.readLine()) !=
	 * null) { sb.append(temp); } } catch (Exception e) { e.printStackTrace(); }
	 * return sb.toString();
	 * 
	 * 
	 * }
	 */
}
